

package com.sixfoot.adminsys.frames.user;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.text.SimpleDateFormat;


public class UserCellRenderers {

    //<snip>Render table rows with alternating colors
    public static class RowRenderer extends DefaultTableCellRenderer {
        private Color rowColors[];

        public RowRenderer() {
            // initialize default colors from look-and-feel
            rowColors = new Color[1];
            rowColors[0] = UIManager.getColor("Table.background");
        }

        public RowRenderer(Color colors[]) {
            super();
            setRowColors(colors);
        }

        public void setRowColors(Color colors[]) {
            rowColors = colors;
        }

        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (column == 2 && value != null) {
                SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                setText(dateFormater.format(value));
            } else {
                setText(value != null ? value.toString() : "");
            }
            if (!isSelected) {
                setBackground(rowColors[row % rowColors.length]);
            }
            return this;
        }

        public boolean isOpaque() {
            return true;
        }
    }

}
